/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.workflow.price;


import com.bifrost.constant.ServicePricingModel;
import com.bifrost.entity.Order;
import com.bifrost.entity.OrderItem;
import com.bifrost.entity.OrderItemService;
import com.bifrost.service.StoreService;
import com.bifrost.workflow.BaseActivity;
import com.bifrost.workflow.ProcessContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 计算订单服务相关的价格
 *
 * @author joewee
 * @version 1.0.0
 * @Date 2017/11/14 13:53
 */
@Component("serviceActivity")
public class ServiceActivity extends BaseActivity<ProcessContext<Order>> {
    @Autowired
    StoreService storeService;

    public static final int ORDER = 2000;

    public ServiceActivity() {
        setOrder(ORDER);
    }

    @Override
    public ProcessContext<Order> execute(ProcessContext<Order> context) throws Exception {
        Order order = context.getSeedData();
        Long serviceShop = order.getServiceShop();
        Boolean needService = order.isNeedService();
        BigDecimal serviceFee = BigDecimal.ZERO;
        if (Boolean.TRUE.equals(needService)) {
            List<OrderItemService> services = order.getOrderItems().stream().filter(orderItem -> orderItem.getHasService()).map(OrderItem::getOrderItemServices).flatMap(List::stream).collect(Collectors.toList());
            //List<ServiceModel> serviceModelList =order.getServices();
            if (serviceShop != null) {
                String pricingModel = storeService.getMerchantStorePricingModel(serviceShop);
                //服务费取最大值
                if (ServicePricingModel.MAX.equalsIgnoreCase(pricingModel)) {
                    Map<Long, List<OrderItemService>> map = services.stream().collect(Collectors.groupingBy(itemService -> itemService.getProductService().getServiceId()));
                    Iterator<Map.Entry<Long, List<OrderItemService>>> iterator = map.entrySet().iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<Long, List<OrderItemService>> entry = iterator.next();
                        List<OrderItemService> serviceModels = entry.getValue();
                        BigDecimal maxPricce = serviceModels.stream().max(new Comparator<OrderItemService>() {
                            @Override
                            public int compare(OrderItemService o1, OrderItemService o2) {
                                return o1.getServicePrice().compareTo(o2.getServicePrice());
                            }
                        }).get().getServicePrice();
                        serviceFee = serviceFee.add(maxPricce);
                    }
                }
                /**
                 *未设置为最大值则默认为sum
                 * */
                else {
                    serviceFee = services.stream().map(OrderItemService::getServicePrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                }

            }/**
             *
             *还未包含门店信息取默认
             */
            else {
                serviceFee = services.stream().map(OrderItemService::getServicePrice).reduce(BigDecimal.ZERO, BigDecimal::add);
            }
            /*if(serviceFee.compareTo(BigDecimal.ZERO)<=0){
                throw new PricingException("服务费计算异常");
            }*/
            order.setTotalService(serviceFee);
        } else {
            order.setTotalService(BigDecimal.ZERO);
        }
        return context;
    }
}
